// pages/Order/showTicketOrder/index.js
const common = require('../../../public.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    pageNum: 1,
    status: '0', // ' '=全部 ，1、未付款，2、已付款，5、交易成功，6、交易关闭
    orderList: [],
    refreshing: false,
    isLoadingBusy: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this
    if (options.status) {
      this.setData({
        status: options.status,
        active: options.status
      })
    }
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid
        }, () => {
          that.reloadData()
        })
      }
    })
  },
  getOrderList(refresh = false) {
    const that = this
    // 当前正在加载或没有获取到用户信息，不再加载数据
    if (this.data.isLoadingBusy || !this.data.userInfo) {
      return
    }
    // 是否是在切换tab页，切换tab页不显示下拉刷新提示
    if (refresh) {
      this.setData({
        noMoreData: false,
        refreshing: true,
        isLoadingBusy: true
      })
    } else {
      if (this.data.noMoreData === true) {
        return
      }
      this.setData({
        isLoadingBusy: true,
        refreshing: false
      })
    }

    const pageNum = refresh ? 1 : this.data.pageNum + 1
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      pageNum,
      pageSize: 10,
      status: that.data.status == 0 ? '' : that.data.status,
    }
    common.post('/ticketOrder/getMyTicketOrderList', tempJson, function (res) {
      if (res.status == 200) {
        // 更新页码
        that.data.pageNum = pageNum
        that.setData({
          orderList: refresh ? res.data : [...that.data.orderList, ...res.data]
        })
        if (res.data === null || res.data.length === 0) {
          that.setData({
            noMoreData: true
          })
        }
      } else {
        common.showRequestErrMsg(res)
      }
    },
      function () {
        that.setData({
          isLoadingBusy: false
        })
        if (refresh) {
          that.setData({
            refreshing: false
          })
        }
      })
  },

  watchEticket(e) {
    let {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaETicket/index?orderNo=' + item.orderNo,
    })
  },
  toOrderDetail(e) {
    let {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaOrderDetail/index?orderNo=' + item.orderNo,
    })
  },
  payOrder: common.throttleFunc(function (e) {
    const that = this
    console.log(e)
    let {
      item
    } = e.currentTarget.dataset

    let tempJson = {
      orderId: item.id,
      userId: that.data.userInfo.id,
      token: that.data.token,
      openid: that.data.openid
    }
    common.wxPayRequest('/wxxcxPayForScenicTicket', tempJson, function (res) {
      wx.requestPayment({
        'timeStamp': res.data.timeStamp,
        'nonceStr': res.data.nonceStr,
        'package': res.data.package,
        'signType': res.data.signType,
        'paySign': res.data.paySign,
        'success': function (res) {
          common.showToast('支付成功', 'success')
          that.reloadData()
        },
        'fail': function (res) {
          if (res.errMsg === 'requestPayment:fail cancel') {
          } else {
            common.showToast(res.errMsg)
          }
        }
      })
    })
  }, 1000),
  deleteClosedOrder(e) { // 删除已经关闭订单
    const that = this
    common.showAlertView('提示', '是否删除该订单？', function (res) {
      if (res) {
        let {
          item
        } = e.currentTarget.dataset
        let tempJson = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderNo: item.orderNo
        }
        common.post('/ticketOrder/delTicketOrder', tempJson, function (res) {
          if (res.status == 200) {
            common.showToast('删除成功')
            that.reloadData()
          } else {
            common.showRequestErrMsg(res)
          }
        })
      }
    })
  },
  onChange(e) {
    this.data.status = e.detail.name
    this.reloadData(true)
  },
  reloadData() {
    this.setData({
      orderList: [],
    }, _ => {
      this.getOrderList(true)
    })
  },
  loadMore() {
    this.getOrderList(false)
  }
})